package com.ojoin.ojoin.eb_spider;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
//import org.openqa.selenium.By;
//import org.openqa.selenium.JavascriptExecutor;
//import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.firefox.FirefoxProfile;
//import org.openqa.selenium.firefox.internal.ProfilesIni;
//import org.openqa.selenium.remote.DesiredCapabilities;
//import org.openqa.selenium.remote.RemoteWebDriver;
//import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Function;

//http://www.lianhanghao.com/index.php
public class SyncUnionBankCodeCase {
	
//	private WebDriver driver = null;
//
//	private List<String> paramUrls=new ArrayList();
//
//	@Before
//	public void setUp(){
//		ProfilesIni allProfiles = new ProfilesIni();
//		FirefoxProfile profile = allProfiles.getProfile("default");
//
//		profile.setPreference("browser.download.folderList", 2);
//		profile.setPreference("browser.helperApps.alwaysAsk.force", false);
//		profile.setPreference("browser.download.manager.showWhenStarting",
//				false);
//		profile.setPreference("browser.download.dir", "E:\\");
//		profile.setPreference(
//				"browser.helperApps.neverAsk.saveToDisk",
//				"application/vnd.ms-excel,application/octet-stream,text/plain,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//
//		profile.setPreference("browser.startup.homepage_override.mstone", "ignore");
//		profile.setPreference("startup.homepage_welcome_url.additional",  "about:blank");
//		profile.setPreference("xpinstall.signatures.required", false);
//
//		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
//		capabilities.setCapability(FirefoxDriver.PROFILE, profile);
//		String gridHub = "http://192.168.0.110:8880/wd/hub";
//		try {
//			driver = new RemoteWebDriver(new URL(gridHub), capabilities);
//		} catch (MalformedURLException e) {
//			e.printStackTrace();
//		}
//
//		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//		driver.manage().window().maximize();
//	}
//
//	@Test
//	public void prepareParamUrl(){
//
//		driver.get("http://www.lianhanghao.com/index.php");
//		waitForPageLoad(driver);
//
//		//http://www.lianhanghao.com/index.php?bank=1&key=&province=1&city=35&page=2
//
//		WebElement bank = driver.findElement(By.id("bank"));
//		Select selectBank = new Select(bank);
//		List<WebElement> bankOptions = selectBank.getOptions();
//
//		WebElement province = driver.findElement(By.id("province"));
//		Select selectProvince = new Select(province);
//		List<WebElement> provinceOptions = selectProvince.getOptions();
//
//		WebElement cnapsSubmit = driver.findElement(By.id("cnapsSubmit"));
//
//		for (int b=1;b<bankOptions.size();b++) {
//			selectBank.selectByValue(String.valueOf(b));
//
//			for (int p=1;p<provinceOptions.size();p++) {
//				selectProvince.selectByValue(String.valueOf(p));
//				waitForAjaxOver(driver);
//
//				WebElement city = driver.findElement(By.id("city"));
//				Select selectCity = new Select(city);
//				List<WebElement> cityOptions = selectCity.getOptions();
//
//				paramUrls.addAll(cityUrl(selectBank, selectProvince, cnapsSubmit, selectCity, cityOptions));
//			}
//		}
//	}
//
//	protected List<String> cityUrl(Select selectBank, Select selectProvince, WebElement cnapsSubmit, Select selectCity,
//			List<WebElement> cityOptions) {
//		List<String> paramUrls=new ArrayList();
//		for(int c=1;c<cityOptions.size();c++){
//			String value=cityOptions.get(c).getAttribute("value");
//			if(null==value)
//				continue;
//
//			selectCity.selectByValue(value);
//			//after click ,this will reload page cause selectBank invalid
//			System.out.println(String.format("银行%s 省份%s 城市%s",
//					selectBank.getFirstSelectedOption().getText(),
//					selectProvince.getFirstSelectedOption().getText(),
//					selectCity.getFirstSelectedOption().getText()
//					));
//
//			paramUrls.add(String.format("http://www.lianhanghao.com/index.php?bank=%s&key=&province=%s&city=%s&page=$",
//					selectBank.getFirstSelectedOption().getAttribute("value"),
//					selectProvince.getFirstSelectedOption().getAttribute("value"),
//					selectCity.getFirstSelectedOption().getAttribute("value")
//					));
//
//			System.out.println(String.format("http://www.lianhanghao.com/index.php?bank=%s&key=&province=%s&city=%s&page=$",
//					selectBank.getFirstSelectedOption().getAttribute("value"),
//					selectProvince.getFirstSelectedOption().getAttribute("value"),
//					selectCity.getFirstSelectedOption().getAttribute("value")
//					));
//		}
//		return paramUrls;
//	}
//
//	public void syncUnionBankCodeInit(){
//
//	}
//
//
//	protected List<WebElement> parseData(WebDriver driver,String bank) {
//		List<WebElement>  unionBankRecord = driver.findElements(By.cssSelector(".tbdata>table tbody tr"));
//		for (WebElement trE : unionBankRecord) {
//			WebElement unionBankCode = trE.findElement(By.cssSelector("td:nth-of-type(2)"));
//			WebElement branchBankName = trE.findElement(By.cssSelector("td:nth-of-type(3)"));
//			WebElement branchBankAddr = trE.findElement(By.cssSelector("td:nth-of-type(5)"));
//
//			String sql=String.format(
//					"INSERT INTO `tb_branch` VALUES ("
//					+ "'%s', "
//					+ "'%s', '7700', '103770096209');"
//					,
//					bank,
//					branchBankName.getText(),
//					unionBankCode.getText().substring(3, 7),
//					unionBankCode.getText()
//					);
//
//			System.out.println(sql);
//		}
//		return unionBankRecord;
//	}
//
//	public void waitForAjaxOver(WebDriver driver) {
//		WebDriverWait wait = new WebDriverWait(driver, 20);
//		wait.until(isAjaxActive());
//	}
//
//	private Function<WebDriver, Boolean> isAjaxActive() {
//		return new Function<WebDriver, Boolean>() {
//			public Boolean apply(WebDriver driver) {
//				JavascriptExecutor executor = (JavascriptExecutor) driver;
//				if ((Boolean) executor
//						.executeScript("return window.jQuery != undefined")) {
//					while (!(Boolean) executor
//							.executeScript("return jQuery.active == 0")) {
//						try {
//							Thread.sleep(1000);
//						} catch (InterruptedException e) {
//							return false;
//						}
//					}
//				}
//				return true;
//			}
//		};
//	}
//
//	public void waitForPageLoad(WebDriver driver) {
//		WebDriverWait wait = new WebDriverWait(driver, 100);
//		wait.until(isPageLoaded());
//	}
//
//	protected Function<WebDriver, Boolean> isPageLoaded() {
//		return new Function<WebDriver, Boolean>() {
//			public Boolean apply(WebDriver driver) {
//				return ((JavascriptExecutor) driver).executeScript(
//						"return document.readyState").equals("complete");
//			}
//		};
//	}
}
